package com.augurit.wf.app.sendorg.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtEditorGridSupport;
import com.augurit.common.base.extmodel.ExtEditorGridResult;
import com.augurit.common.util.ExtUtils;
import com.augurit.wf.app.sendorg.service.IOaSendOrgService;
import com.augurit.wf.app.sendorg.web.form.OaSendOrgForm;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "oa-send-org.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/wf/app/sendorg/oa-send-org.jsp")
})
public class OaSendOrgAction extends ExtEditorGridSupport<OaSendOrgForm> {

	@Autowired
	private IOaSendOrgService oaSendOrgService;

	private Page<OaSendOrgForm> page = new Page<OaSendOrgForm>(15);


	/**
	 * 系统默认调用入口
	 */
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	@Override
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		oaSendOrgService.search(page, getFormForSearch());
		this.extRenderGridJson(page);
		return null;
	}

	@Override
	public String create() throws Exception {
		List<OaSendOrgForm> list = persistChangeForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridCreateMessage(list), list, false);
		return null;
	}

	@Override
	public String update() throws Exception {
		List<OaSendOrgForm> list = persistChangeForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridUpdateMessage(list), list, true);
		return null;
	}

	@Override
	public String delete() throws Exception {
		Long[] ids = result.getDeletedIds();
		oaSendOrgService.delete(ids);
		this.extRenderEditorGridResult(true, ExtEditorGridResult.getEditorGridDeleteMessage(ids));
		return null;
	}

	private List<OaSendOrgForm> persistChangeForms() throws Exception{
		List<OaSendOrgForm> list = result.getChangedForms(OaSendOrgForm.class);	
		oaSendOrgService.save(list);
		return list;
	}

	private OaSendOrgForm getFormForSearch(){
		OaSendOrgForm form = new OaSendOrgForm();
		form.setOrgName(getParameter("orgName"));
		return form;
	}
}
